package View;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class BufferedImagePanel extends JPanel {
	private BufferedImage bi;
	private Image loadImage;
	private boolean loadEnabled = false;

	public BufferedImagePanel(){
		bi = null;
	}
	
	public BufferedImagePanel(BufferedImage bi, int x, int y) {
		this.bi = bi;
		this.setSize(x, y);
		this.setPreferredSize(getSize());
	}

	public void paint(Graphics g) {
		super.paintComponents(g);

		try {
			if(bi != null)
				g.drawImage(bi, 0, 0, getSize().width, getSize().height, this);
			else if(loadEnabled){
				g.drawImage(loadImage, 0, 0, getSize().width, getSize().height, this);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public BufferedImage getBi() {
		return bi;
	}

	public void setBi(BufferedImage bi) {
		this.loadEnabled = false;
		this.bi = bi;
		this.repaint();
	}
	
	private void loadImage(){
			URL url = ClassLoader.getSystemResource("data/ajax-loader.gif");
			loadImage = new ImageIcon(url).getImage(); 
}

	public Image getLoadImage() {
		return loadImage;
	}

	public void setLoadImage(Image loadImage) {
		this.loadImage = loadImage;
	}

	public boolean isLoadEnabled() {
		return loadEnabled;
	}

	public void setLoadEnabled(boolean loadEnabled) {
		this.loadEnabled = loadEnabled;
		if(loadEnabled == true)
			loadImage();
	}
	
}
